/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.piaohao.util.time;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author Administrator
 */
public class TimeUtil {

    /**
     * 返回当前时间的字符串
     *
     * @param joinChar 连接各单位的字符，如"_",":",""
     * @return
     */
    public static String getNow(String joinChar) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        String m = "";
        if (month < 10) {
            m = "0" + month;
        } else {
            m = "" + month;
        }
        String d = "";
        int date = c.get(Calendar.DATE);
        if (date < 10) {
            d = "0" + date;
        } else {
            d = "" + date;
        }
        return year + joinChar
                + m + joinChar
                + d;
    }

    /**
     * 返回当前时间的字符串
     *
     * @param joinChar 连接各单位的字符，如"_",":",""
     * @return
     */
    public static String getNowWithHMS(String joinChar1, String joinChar2) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        return year + joinChar1
                + month + joinChar1
                + date + " "
                + hour + joinChar2
                + minute + joinChar2
                + second;
    }

    public static Date generateBirthday() {
        Random rand = new Random();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        cal.set(year - 70, 0, 1);
        long start = cal.getTimeInMillis();
        cal.set(year - 20, 0, 1);
        long end = cal.getTimeInMillis();
        return new Date(start + (long) (rand.nextDouble() * (end - start)));
    }

    public static Date generateVehRegDate() {
        Random rand = new Random();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        cal.set(year - 5, 0, 1);
        long start = cal.getTimeInMillis();
        cal.set(year, 0, 1);
        long end = cal.getTimeInMillis();
        return new Date(start + (long) (rand.nextDouble() * (end - start)));
    }

    public static Date generateLineStartDate() {
        Random rand = new Random();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        cal.set(year - 50, 0, 1);
        long start = cal.getTimeInMillis();
        cal.set(year, 0, 1);
        long end = cal.getTimeInMillis();
        return new Date(start + (long) (rand.nextDouble() * (end - start)));
    }

    public static void main(String[] args) {
        System.out.println(TimeUtil.generateBirthday());
    }
}
